<?php


namespace App\Exports;


use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\Exportable;

class MultipleDataExport implements WithMultipleSheets
{
    use Exportable, ExportTrait;

    private $model;
    private $callback;
    private $perNum = 1000;

    public function setData(Builder $model,callable $callback)
    {
        $this->model = $model;
        $this->callback = $callback;
        return $this;
    }

    public function setPerNum($num)
    {
        $this->perNum = $num;
        return $this;
    }

    public function sheets(): array
    {
        $sheets = [];
        $flag = true;
        $step = 1;
        while ($flag){
            $data = $this->model->forPage($step,$this->perNum)->get();
            if($data->isEmpty()){
                break;
            }
            $step +=1;
            $sheets[] = (new DataExport())->setHeadings($this->title,$this->headings)
                ->setFormats($this->columnFormat)
                ->filterData($data,$this->callback);
        }
        return $sheets;
    }
}